<template>
	<div class="accideporting">
		<div class="from">
			<el-form label-position="top" label-width="auto" :model="prop">
				<el-form-item label="事故地点">
					<el-input v-model="prop.accidenttion" disabled />
				</el-form-item>
				<el-form-item label="事故名称">
					<el-input v-model="prop.accidename" disabled />
				</el-form-item>
				<el-form-item label="事故类型">
					<el-input :value="getStateText(prop.acctype)" disabled />
				</el-form-item>
				<el-form-item label="伤亡情况">
					<el-input v-model="prop.casualties" disabled />
				</el-form-item>
				<el-button type="success" @click="registerForm()">任务完成</el-button>
			</el-form>
		</div>
	</div>
</template>

<script setup>
	import {
		Plus
	} from "@element-plus/icons-vue";
	import {
		useAuthStore
	} from "../../../store/auth";
	import {
		reactive,
		ref,
		onMounted,
		defineExpose,
		computed
	} from "vue";
	import {
		ElMessage
	} from "element-plus";
	import axios from "axios";
	import {
		getteamPublist,
		updateTeamStateToZero,
		updatepulistStateTothree
	} from "../../../api/d_user/setup/index.js"


	const loginuser = useAuthStore();

	const prop = reactive({
		id: "",
		accidename: '',
		acctype: '',
		accidenttion: '',
		casualties: '',
	});


	const acctype = {
		0: "轻微事故",
		1: "小型事故",
		2: "一般事故",
		3: "大型事故",
		4: "重大事故",
	};

	const getStateText = (state) => {
		return acctype[state];
	};

	const bakQueryprop = JSON.stringify(prop);


	const registerForm = async () => {
		updateTeamStateToZero(
			loginuser.user.username
		).then((response) => {
			if (response.data.status === 1) {
				console.log(prop)
				updatepulistStateTothree(prop.id).then (
					(iteam) => {
						if (iteam.data.status === 1) {
							ElMessage({
								showClose: true,
								message: "上报成功",
								type: "success",
							})
							Object.assign(prop, JSON.parse(bakQueryprop));
							uni.switchTab({
								url: '/pages/o_user/setup/index'
							})
						} else {
							ElMessage.error("上报失败，请稍后重试");
						}
					}
				)
			} else {
				ElMessage.error("上报失败，请稍后重试");
			}
		})
	}

	async function handleQuery() {
		await getteamPublist(loginuser.user.username).then((item) => {
			prop.accidename = item.data.accidename;
			prop.accidenttion = item.data.accidenttion;
			prop.acctype = item.data.acctype;
			prop.casualties = item.data.casualties;
			prop.id = item.data.id;
		})
	}
	onMounted(() => {
		handleQuery();
	});
</script>

<style lang="scss">
	.accideporting {
		width: 100%;
		height: 100vh;
		display: flex;
		justify-content: center;
	}

	.from {
		margin-top: 5%;
		width: 80%;
	}
</style>